/* server端 */


.LC0:
	.string "127.0.0.1"

.LC1:
	.string "error\n"

.LC2:
	.string "%d:%s\n"

.LC3:
	.string "OK"

.LC4:
	.string "one client over\n"

.globl main


main:
	pushq %rbp
	movq %rsp,%rbp
	subq $0x1000,%rsp

	movl $0,-0x1000(%rbp) /* sockfd */
	movl $0,-0xff8(%rbp) /* ser */ 
	movl $0,-0xfe8(%rbp) /* cli */
	movl $0,-0xfd8(%rbp) /* res */
	movl $0,-0xfd4(%rbp) /* len */
	movl $0,-0xfd0(%rbp) /* c */
	movl $0,-0xfcc(%rbp) /* n */
	movl $0,-0xfc8(%rbp) /* recvbuf */
	
	movl $0,%edx
	movl $1,%esi 
	movl $2,%edi 
	movl $0,%eax 
	call socket 
	movl %eax,-0x1000(%rbp) /* sockfd */

	movl $16,%edx
	movl $0,%esi 
	leaq -0xff8(%rbp),%rdi /* ser */
	movl $0,%eax 
	call memset 

	leaq -0xff8(%rbp),%rdi /* ser */
	movw $2,0(%rdi) /* ser.sin_family */

	movl $6000,%edi 
	movl $0,%eax 
	call htons 

	leaq -0xff8(%rbp),%rdi /* ser */
	movw %ax,2(%rdi) /* ser.sin_port */ 

	leaq .LC0(%rip),%rdi
	movl $0,%eax
	call inet_addr 

	leaq -0xff8(%rbp),%rdi /* ser */
	movl %eax,4(%rdi) /* ser.sin_addr.s_addr */

	movl $16,%edx 
	leaq -0xff8(%rbp),%rsi /* ser */
	movl -0x1000(%rbp),%edi /* sockfd */
	movl $0,%eax
	call bind
	movl %eax,-0xfd8(%rbp) /* res */

	movl $5,%esi 
	movl -0x1000(%rbp),%edi /* sockfd */
	movl $0,%eax
	call listen

	jmp .Lmain_400

.Lmain_500:
	movl $16,-0xfd4(%rbp) /* len */

	leaq -0xfd4(%rbp),%rdx /* len */
	leaq -0xfe8(%rbp),%rsi /* cli */
	movl -0x1000(%rbp),%edi /* sockfd */
	movl $0,%eax 
	call accept 
	movl %eax,-0xfd0(%rbp) /* c */

	cmpl $0,-0xfd0(%rbp) /* c */
	jge .Lmain_490

	leaq .LC1(%rip),%rdi
	movl $0,%eax 
	call perror 

	jmp .Lmain_500 /* continue */

.Lmain_490:
	jmp .Lmain_450

.Lmain_489:
	movl $128,%edx
	movl $0,%esi 
	leaq -0xfc8(%rbp),%rdi  /* recvbuf */
	movl $0,%eax
	call memset 

	movl $0,%ecx
	movl $127,%edx 
	leaq -0xfc8(%rbp),%rsi /* recvbuf */
	movl -0xfd0(%rbp),%edi /* c */
	movl $0,%eax 
	call recv 
	movl %eax,-0xfcc(%rbp) /* n */
	cmpl $0,-0xfcc(%rbp) /* n */
	jg .Lmain_470

	leaq .LC4(%rip),%rdi
	movl $0,%eax 
	call printf 

	jmp .Lmain_440 /* break */

.Lmain_470:
	leaq -0xfc8(%rbp),%rdx /* recvbuf */
	movl -0xfd0(%rbp),%esi /* c */
	leaq .LC2(%rip),%rdi
	movl $0,%eax 
	call printf 

	movl $0,%ecx
	movl $2,%edx 
	leaq .LC3(%rip),%rsi /* */
	movl -0xfd0(%rbp),%edi /* c */
	movl $0,%eax 
	call send 

.Lmain_450:
	jmp .Lmain_489
	
.Lmain_440:
	movl -0xfd0(%rbp),%edi /* c */
	movl $0,%eax 
	call close 

.Lmain_400:
	jmp .Lmain_500
	
	movl -0x1000(%rbp),%edi /* sockfd */
	movl $0,%eax 
	call close 

	movl $0,%eax
	leave
	ret
